package dk;

import com.alibaba.fastjson.JSONObject;
import ocg.http.httpcore.Http;
import ocg.http.httpcore.HttpFactory;
import ocg.http.httpcore.HttpModel;
import ocg.http.httpcore.HttpResponse;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chenbo on 2018/5/8.
 */
public class DK {


    @Test
    public void login(){

        HttpModel model = Http.post ("https://benlai.gaiaworkforce.com/v3.0/api/Login/ValidateUser")
                .header ( "X-Tingyun-Lib-Type-N-ST","0;1525777263049" )
                .header ( "Content-Type","application/json; charset=utf-8" )
                .header ( "User-Agent","Dalvik/2.1.0 (Linux; U; Android 7.0; Redmi Note 4X MIUI/V9.5.2.0.NCFCNFA)" )
                .header ( "Host","benlai.gaiaworkforce.com" )
                .header ( "Connection","Keep-Alive" )
                .header ( "Accept-Encoding","gzip" )
                .body ( "{\n" +
                        "\t\"loginName\": \"nLe9iIyoluo=\",\n" +
                        "\t\"password\": \"zrPz9raqn8o8ljhyoOzv+A==\",\n" +
                        "\t\"simId\": \"80AE1E124F80A6F9509DFA6B95B528E5|AE2F70D537089FAC77B11732D61CF419\",\n" +
                        "\t\"mobileId\": \"25AD431BF0DC30CFD97D470CDD5DA0AA\",\n" +
                        "\t\"phoneModel\": \"Xiaomi Redmi Note 4X\",\n" +
                        "\t\"phoneSysVersion\": \"7.0\",\n" +
                        "\t\"appVersion\": \"3.9.3.11\",\n" +
                        "\t\"language\": \"ZH-CN\",\n" +
                        "\t\"isClearGesturePwd\": 0,\n" +
                        "\t\"companyCode\": \"benlai\",\n" +
                        "\t\"registrationId\": \"1507bfd3f7fe52fef6a\",\n" +
                        "\t\"resolution\": \"1080x1920\",\n" +
                        "\t\"loginNum\": 0,\n" +
                        "\t\"sessionCode\": \"07f87792-f690-4d90-81e2-19828c70db81\"\n" +
                        "}" );

        HttpResponse response = HttpFactory.factory ( model );
        JSONObject json = response.jsonBody ();

        JSONObject data = json.getJSONObject ( "data" );
        String SessionId = data.getString ( "SessionId" );

        Submit ( SessionId );
    }

    /**
     {
     "result": true,
     "data": {
     "IsValid": true,
     "SysParaInfo": null,
     "ModuleList": [
     {
     "Id": "S010010",
     "Name": "移动打卡"
     },
     {
     "Id": "S010020",
     "Name": "我的排班"
     },
     {
     "Id": "S010030",
     "Name": "请假申请"
     },
     {
     "Id": "S010040",
     "Name": "加班申请"
     },
     {
     "Id": "S010060",
     "Name": "我的表单"
     },
     {
     "Id": "S010070",
     "Name": "主管审核"
     },
     {
     "Id": "S010080",
     "Name": "考勤统计"
     },
     {
     "Id": "S010160",
     "Name": "排班查询"
     },
     {
     "Id": "S010170",
     "Name": "销假申请"
     }
     ],
     "Uuid": "23A01AF0-232A-4518-9C0E-323FB773F5EF",
     "PunchTypeList": [
     {
     "PunchType": "APP_GPS"
     },
     {
     "PunchType": "APP_BHT"
     }
     ],
     "gesturePwd": "",
     "timeClassS": "09:00",
     "timeClassE": "18:00",
     "ServiceTime": "19:13",
     "BackGroundVersion": "Y1",
     "aghUrl": null,
     "CompanyName": null,
     "CompanyEngName": null,
     "SessionId": "daa0db65-fdfb-47d1-8e1a-6b2334cabb3c",
     "UserID": "3235adbb-91b6-4d63-bc67-235d62f96a5f",
     "PersonID": "fba822c4-865d-405c-8028-03826de5b368",
     "LoginName": null,
     "EmpID": null,
     "EmpName": null,
     "EnglishName": null,
     "DeptName": null,
     "DeptId": null,
     "Gender": null,
     "BirthDay": null,
     "Position": null,
     "HeadImgUrl": null,
     "EmployeeTypeId": "16_华东区",
     "phoneNumber": null
     }
     }
     */

    public void Attendance( String SessionId){
        HttpModel model = Http.post ("https://benlai.gaiaworkforce.com/v3.0/api/v5/Attendance/MatchOptimalBHT")
                .header ( "X-Tingyun-Lib-Type-N-ST","0;1525777263049" )
                .header ( "Content-Type","application/json; charset=utf-8" )
                .header ( "User-Agent","Dalvik/2.1.0 (Linux; U; Android 7.0; Redmi Note 4X MIUI/V9.5.2.0.NCFCNFA)" )
                .header ( "Host","benlai.gaiaworkforce.com" )
                .header ( "Connection","Keep-Alive" )
                .header ( "Accept-Encoding","gzip" )
                .body ( "{\n" +
                        "\t\"SessionId\": \""+SessionId+"\",\n" +
                        "\t\"Longitude\": \"\",\n" +
                        "\t\"Latitude\": \"\",\n" +
                        "\t\"isNeedLocation\": false,\n" +
                        "\t\"isNeedPunchCard\": false,\n" +
                        "\t\"isOutWork\": true,\n" +
                        "\t\"appBhinfos\": [{\n" +
                        "\t\t\"Major\": 10055,\n" +
                        "\t\t\"Minor\": 5261\n" +
                        "\t}],\n" +
                        "\t\"sessionCode\": \""+SessionId+"\"\n" +
                        "}" );

        HttpResponse response = HttpFactory.factory ( model );
    }

    public void Submit(String SessionId){

        String data = data ();

        HttpModel model = Http.post ("https://benlai.gaiaworkforce.com/v3.0/api/Attendance/SubmitEmployeeAttendanceDataByBHT")
                .header ( "X-Tingyun-Lib-Type-N-ST","0;1525777263049" )
                .header ( "Content-Type","application/json; charset=utf-8" )
                .header ( "User-Agent","Dalvik/2.1.0 (Linux; U; Android 7.0; Redmi Note 4X MIUI/V9.5.2.0.NCFCNFA)" )
                .header ( "Host","benlai.gaiaworkforce.com" )
                .header ( "Connection","Keep-Alive" )
                .header ( "Accept-Encoding","gzip" )
                .body ( "{\n" +
                        "\t\"ClockType\": \"APP_BHT\",\n" +
                        "\t\"Major\": \"10055\",\n" +
                        "\t\"Minor\": \"5261\",\n" +
                        "\t\"Longitude\": \"\",\n" +
                        "\t\"Latitude\": \"\",\n" +
                        "\t\"Address\": \"\",\n" +
                        "\t\"Remark\": \"\",\n" +
                        "\t\"SessionId\": \""+SessionId+"\",\n" +
                        "\t\"sign\": \"75665c8de31e5836974fc261176ee2b96db3d29605eb3ef9d9100b2664bb6d45\",\n" +
                        "\t\"timeStamp\": \""+data+"\",\n" +
                        "\t\"sessionCode\": \""+SessionId+"\"\n" +
                        "}" );

        HttpResponse response = HttpFactory.factory ( model );

        JSONObject object = response.jsonBody ();

        if ( object.getBoolean ( "result" )){
            JSONObject da = object.getJSONObject ( "data" );
            Assert.assertEquals ( "打卡成功",da.getString ( "result" ) );
        }


    }


    public String data(){
        Date date = new Date();
        return new SimpleDateFormat ("yyyyMMddHHmmss").format(date);
    }
}
